package GUILayer;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;

import ControlLayer.SaleCTR;
import ModelLayer.Sale;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SaleGUI extends JFrame {

	private JPanel contentPane;
	private JTextField textID;
	private JTextField textSerialNumber;
	private JTextField textStaffPhone;
	private JTextField textCustomerPhone;
	private JTextField textSaleID;
	private SaleCTR salCTR;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SaleGUI frame = new SaleGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SaleGUI() {
		salCTR = new SaleCTR();
		setTitle("Sale UI");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBackground(Color.GRAY);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel PanelEnterInformations = new JPanel();
		PanelEnterInformations.setLayout(null);
		PanelEnterInformations.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0)), "Enter Information", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(255, 255, 255)));
		PanelEnterInformations.setBackground(Color.GRAY);
		PanelEnterInformations.setBounds(10, 32, 547, 183);
		contentPane.add(PanelEnterInformations);
		
		JButton btnAddSale = new JButton("Add Sale");
		btnAddSale.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				createSale();		
			}
		});
		btnAddSale.setToolTipText("Add a Sale to the database");
		btnAddSale.setBounds(385, 147, 150, 25);
		PanelEnterInformations.add(btnAddSale);
		
		JLabel lblId = new JLabel("ID:");
		lblId.setHorizontalAlignment(SwingConstants.CENTER);
		lblId.setForeground(Color.WHITE);
		lblId.setBounds(10, 24, 100, 20);
		PanelEnterInformations.add(lblId);
		
		JLabel lblSerialNumber = new JLabel("Serial Number:");
		lblSerialNumber.setHorizontalAlignment(SwingConstants.CENTER);
		lblSerialNumber.setForeground(Color.WHITE);
		lblSerialNumber.setBounds(145, 24, 100, 20);
		PanelEnterInformations.add(lblSerialNumber);
		
		JLabel lblCustomerPhone = new JLabel("Customer Phone:");
		lblCustomerPhone.setHorizontalAlignment(SwingConstants.CENTER);
		lblCustomerPhone.setForeground(Color.WHITE);
		lblCustomerPhone.setBounds(290, 24, 100, 20);
		PanelEnterInformations.add(lblCustomerPhone);
		
		JLabel lblStaffPhone = new JLabel("Staff Phone:");
		lblStaffPhone.setHorizontalAlignment(SwingConstants.CENTER);
		lblStaffPhone.setForeground(Color.WHITE);
		lblStaffPhone.setBounds(435, 24, 100, 20);
		PanelEnterInformations.add(lblStaffPhone);
		
		JLabel lblSaleId = new JLabel("Sale ID:");
		lblSaleId.setHorizontalAlignment(SwingConstants.CENTER);
		lblSaleId.setForeground(Color.WHITE);
		lblSaleId.setBounds(10, 74, 100, 20);
		PanelEnterInformations.add(lblSaleId);
		
		textID = new JTextField();
		textID.setColumns(10);
		textID.setBounds(10, 47, 100, 20);
		PanelEnterInformations.add(textID);
		
		textSerialNumber = new JTextField();
		textSerialNumber.setColumns(10);
		textSerialNumber.setBounds(145, 47, 100, 20);
		PanelEnterInformations.add(textSerialNumber);
		
		textStaffPhone = new JTextField();
		textStaffPhone.setColumns(10);
		textStaffPhone.setBounds(435, 47, 100, 20);
		PanelEnterInformations.add(textStaffPhone);
		
		textCustomerPhone = new JTextField();
		textCustomerPhone.setColumns(10);
		textCustomerPhone.setBounds(290, 47, 100, 20);
		PanelEnterInformations.add(textCustomerPhone);
		
		textSaleID = new JTextField();
		textSaleID.setColumns(10);
		textSaleID.setBounds(10, 95, 100, 20);
		PanelEnterInformations.add(textSaleID);
		
		JButton button = new JButton("Clear");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				clear();
			}
		});
		button.setBounds(202, 148, 150, 25);
		PanelEnterInformations.add(button);
		setSize(600, 400);
	}
	private void createSale()
	{
		try 
		{
			salCTR.createSale(Integer.parseInt(textID.getText()), Integer.parseInt(textSerialNumber.getText()), Integer.parseInt(textSaleID.getText()), textStaffPhone.getText(), textCustomerPhone.getText());
			System.out.println("");
		} 
		catch (Exception e) 
		{
			System.out.println("Der er sket en fejl");
		}
		 System.out.println("ProductID: " + textID.getText() + " Serialnumber " + textSerialNumber.getText() + " Customer nr: " + textCustomerPhone.getText() + " Staff nr: " + textStaffPhone.getText() + " Staff nr: " + textSaleID.getText() );
	}
	private void clear()
	{
		textID.setText("");
		textSerialNumber.setText("");
		textCustomerPhone.setText("");
		textStaffPhone.setText("");
		textSaleID.setText("");		
	}

	
}
